<template>
    <div v-if="showPage">
        <div @click="routerLinkCompany">集团管理</div>
        <div v-for="row in mallList" @click="routerLinkMall(row.id)">
            {{ row.name }}{{ row.address }}{{ row.id }}
        </div>
    </div>
</template>

<script setup lang="ts">
    import { ref, onMounted } from "vue";
    import { ElMessage } from "element-plus";
    import { useRoute, useRouter } from "vue-router";
    import { _mallStore } from "@/store/config";
    import { getAsideFirstRoute } from "@/store/utils";
    import { getRestfulMalls, RestfulMallsRow, PromiseRestfulMalls } from "@/assets/api";
    import useLoading from "@/hooks/loading";

    const { showLoading, hideLoading } = useLoading();// 加载loading
    const route = useRoute();
    const router = useRouter();

    _mallStore.setCompanyName();
    let mallList = ref<RestfulMallsRow[]>([]);

    let showPage = ref<boolean>(false);
    onMounted(async () => {
        showLoading();
        getRestfulMalls({
            mall_id: -1,
            location_id: 0
        }).then((resp: PromiseRestfulMalls) => {
            if (resp.code === 200) {
                mallList.value = resp.data;
                showPage.value = true;
                hideLoading();
            } else {
                ElMessage.error(resp.msg);
            }
        })
    })

    // 选择进入集团
    const routerLinkCompany = () => {
        _mallStore.$patch({
            mallId: -1,
            system: 4,
            mallEdition: 1,
        });
        routerLink('company');
    }
    // 选择进入商场
    const routerLinkMall = (id: number) => {
        _mallStore.$patch({
            mallId: id,
            system: 5,
            mallEdition: 2,
        });
        routerLink();
    }
    const routerLink = async (type: string = 'mall') => {
        await _mallStore.getResetApiStore();
        const { route } = getAsideFirstRoute(_mallStore.navigationMenus[0]);
        localStorage.setItem('defaultRouterName', route);
        if (type === 'mall') {
            // 显示商场名称
            _mallStore.setMallName();
        } else {
            // 显示集团名称
            _mallStore.setCompanyName();
        }
        try {
            router.push({
                name: route,
            }).catch(() => {
                router.push('/404')
            });
        } catch (e) {
            router.push('/404')
        }
    }
</script>

<style scoped lang="scss">

</style>